/** for Color */
import java.awt.Color;

/** for Arrays */
import java.util.Arrays;

/**
 * HW 12b & 13b, "S_Piece"<br><br>
 * 
 * This class is represents the S tetromino (an S shaped tetromino) for the Piece object
 * 
 * @author Hyun Seo Chung
 * @version 1.0 04/21/12
 */
public class S_Piece extends Piece {
	
	/**
	 * serialVersionUID for the S_Piece class
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * first shape
	 */
	private static final int[][] SHAPE_ONE =
			new int[][] {{0, 1, 1}, {1, 1, 0}, {0, 0, 0}};
	
	/**
	 * second shape
	 */
	private static final int[][] SHAPE_TWO =
			new int[][] {{0, 1, 0}, {0, 1, 1}, {0, 0, 1}};
	
	/**
	 * third shape
	 */
	private static final int[][] SHAPE_THREE =
			new int[][] {{0, 0, 0}, {0, 1, 1}, {1, 1, 0}};

	/**
	 * fourth shape
	 */
	private static final int[][] SHAPE_FOUR =
			new int[][] {{1, 0, 0}, {1, 1, 0}, {0, 1, 0}};

	/**
	 * This is a constructor that takes in a x and y position of the piece to be placed, 
	 * and sets it up (plus the corresponding color and the default shape)
	 *  by calling the super constructor. 
	 * 
	 * @param x The column number of the tile to be checked
	 * @param y The row number of the tile to be checked
	 */
	public S_Piece(int x, int y) {
		super(x, y, Color.GREEN, SHAPE_ONE);
	}

	/**
	 * This method checks and rotates the piece to the next shape.
	 * 
	 * @return true if it was able (allowed) to rotate, false otherwise
	 */
	@Override
	public boolean canRotate() {

		if (Arrays.deepEquals(this.getModel(), SHAPE_ONE)) {
			this.setModel(SHAPE_TWO);
		}
		else if (Arrays.deepEquals(this.getModel(), SHAPE_TWO)) {
			this.setModel(SHAPE_THREE);
		}
		else if (Arrays.deepEquals(this.getModel(), SHAPE_THREE)) {
			this.setModel(SHAPE_FOUR);
		}
		else if (Arrays.deepEquals(this.getModel(), SHAPE_FOUR)) {
			this.setModel(SHAPE_ONE);
		}
		else {
			return false;
		}
		
		return canRotateHelper();
	}
	
	/**
	 * for rotate Helper
	 * @return true otherwise false
	 */
	private boolean canRotateHelper() {
		for ( int i = 0; i < SHAPE_ONE.length; i++ ) {
			for ( int j = 0; j < SHAPE_ONE.length; j++ ) {
				if (this.getModel()[i][j] == 1) {
					if (!PlayingPanel.getField().isTileVacant
							(this.getX() + j, this.getY() + i)) {
						// since it is not possible to rotate, set the model back to what it was
						if (Arrays.deepEquals(this.getModel(), SHAPE_ONE)) {
							this.setModel(SHAPE_FOUR);
						}
						else if (Arrays.deepEquals(this.getModel(), SHAPE_TWO)) {
							this.setModel(SHAPE_ONE);
						}
						else if (Arrays.deepEquals(this.getModel(), SHAPE_THREE)) {
							this.setModel(SHAPE_TWO);
						}
						else if (Arrays.deepEquals(this.getModel(), SHAPE_FOUR)) {
							this.setModel(SHAPE_THREE);
						}
						return false;
					}
				}
			}
		}
		return true;
	}

	/**
	 * This is a tostring method.
	 * @return tostring.
	 */
	@Override
	public String toString() {
		return super.toString();
	}
}
